package org.linlinjava.litemall.db.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.linlinjava.litemall.db.dao.SystemStorageMapper;
import org.linlinjava.litemall.db.models.SystemStorage;
import org.linlinjava.litemall.db.service.SystemStorageService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class SystemStorageServiceImpl extends ServiceImpl<SystemStorageMapper, SystemStorage>
    implements SystemStorageService {

    @Override
    public void deleteByKey(String key) {
        QueryWrapper<SystemStorage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("key", key);
        baseMapper.delete(queryWrapper);
    }

    @Override
    public SystemStorage findByKey(String key) {
        QueryWrapper<SystemStorage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("key", key);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public IPage<SystemStorage> querySelective(String key, String name, Integer pageNo, Integer pageSize) {
        Page<SystemStorage> page = new Page<>(pageNo, pageSize);
        QueryWrapper<SystemStorage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(!StringUtils.isEmpty(key), "key", key);
        queryWrapper.like(!StringUtils.isEmpty(name), "name", name);

        return baseMapper.selectPage(page, queryWrapper);
    }
}
